/*
 * 
 * Copyright (C) 1997 Jayakrishnan Nair jk@csr.uvic.ca
 * WWW: http://www.csc.uvic.ca/~jk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import java.awt.*;

public class DrawnRectangle extends Rectangle {
    protected static int defaultThickness = 2;
    protected Component drawInto;
    private int thick;
    private Color lineColor, fillColor;

    public DrawnRectangle(Component drawInto) {
        this(drawInto,defaultThickness,0,0,0,0);
    }

    public DrawnRectangle(Component drawInto, int thick) {
        this(drawInto,thick,0,0,0,0);
    }

    public DrawnRectangle(Component drawInto,int x, int y,int w,int h) {
        this(drawInto,defaultThickness,x,y,w,h);
    }

    public DrawnRectangle(Component drawInto, int thick, int x, int y, int w, int h) {
        this.drawInto = drawInto;
        this.thick = thick;
        reshape(x,y,w,h);
    }

    public Component component() { return drawInto;}
    public int getThickness() {return thick;}
    public void setThickness(int thick){this.thick = thick;}

    public void setLineColor(Color lineColor) {
        this.lineColor = lineColor;
    }

    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }

    public void fill() {
        fill(getFillColor());
    }

    public Color getLineColor() {
        if(lineColor == null)
            lineColor = drawInto.getBackground().darker().darker().darker();
        return lineColor;
    }


    public Color getFillColor() {
        if(fillColor == null)
            fillColor = drawInto.getBackground();
        return fillColor;
    }

    public Rectangle getInnerBounds() {
        return new Rectangle(x+thick,y+thick,width-(thick*2),height-(thick*2));
    }

    public void paint() {
        Graphics g = drawInto.getGraphics();
        paintFlat(g,getLineColor());
    }

    private void paintFlat(Graphics g,Color c) {
        if(g != null) {
            g.setColor(c);
            for(int i=0; i < thick; i++)
                g.drawRect(x+i,y+i,width-(i*2)-1,height-(i*2)-1);
        }
    }

    public void clearInterior() {
        fill(drawInto.getBackground());
    }

    public void clearExterior() {
        paintFlat(drawInto.getGraphics(),drawInto.getBackground());
    }

    public void clear() {
        clearExterior();
        clearInterior();
    }


    public void fill(Color color) {
        Graphics  g = drawInto.getGraphics();
        if(g != null) {
            Rectangle r = getInnerBounds();
            g.setColor(color);
            g.fillRect(r.x,r.y,r.width,r.height);
            setFillColor(color);
        }
    }


    protected Color brighter() {
        return getLineColor().brighter().brighter().brighter().brighter();
    }
}
